package com.three.order.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
@Component
public class FeignAuthRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        // 获取当前请求的属性，用于后续从请求中提取token
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        // 检查是否成功获取到请求属性
        if (attributes != null) {
            // 从请求头中获取token
            String token = attributes.getRequest().getHeader("token");
            // 如果token存在，则将其作为header添加到template中
            if (token != null) {
                template.header("token", token);
            }
        }
    }
}